<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州瑞璞玉数字科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\form\event;

use addon\form\model\Form;
use app\model\goods\Goods;

/**
 * 订单待付款
 */
class OrderPayment
{
    /**
     * @param $data
     * @return array
     */
    public function handle($data)
    {
        $form_model = new Form();
        $info = $form_model->getFormInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'is_use', '=', 1 ], [ 'form_type', '=', 'order' ] ], 'id,json_data,form_name');
        if (!empty($info[ 'data' ])) {
            $info[ 'data' ][ 'json_data' ] = json_decode($info[ 'data' ][ 'json_data' ], true);
            $data[ 'system_form' ] = $info[ 'data' ];
        }
        $goods_model = new Goods();
        foreach ($data[ 'shop_goods_list' ][ 'goods_list' ] as $goods_key => $goods_item) {
            $condition = [
                [ 'g.site_id', '=', $data[ 'site_id' ] ],
                [ 'g.goods_id', '=', $goods_item[ 'goods_id' ] ],
                [ 'g.form_id', '>', 0 ],
                [ 'f.is_use', '=', 1 ]
            ];
            $goods_form = $goods_model->getGoodsInfo($condition, 'f.id,f.json_data', 'g', [ [ 'form f', 'g.form_id = f.id', 'left' ] ])[ 'data' ];
            if (!empty($goods_form)) {
                $goods_form[ 'json_data' ] = json_decode($goods_form[ 'json_data' ], true);
                $data[ 'shop_goods_list' ][ 'goods_list' ][ $goods_key ][ 'goods_form' ] = $goods_form;
            }
        }
        return $data;
    }
}